package com.webserver.servlet;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class Jdbc {
    private static DruidDataSource druidDataSource=null;
    private static String url;
    private static String username;
    private static String password;
    static {
        try {
            //读取文件
            Properties properties = new Properties();
            InputStream aClass = Jdbc.class.getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(aClass);
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            //
            druidDataSource = new DruidDataSource();
            druidDataSource.setUrl(url);
            druidDataSource.setUsername(username);
            druidDataSource.setPassword(password);
            druidDataSource.setInitialSize(10);
            druidDataSource.setMaxActive(10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try (Connection connection=Jdbc.getCon()){
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery("select * from emp");
            //遍历
            while (set.next()){
                System.out.println(set.getString(4));
            }
            System.out.println("输入完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getCon() {
        Connection connection = null;
        try {
            System.out.println(druidDataSource.getConnection());
            connection = DriverManager.getConnection(url,username ,password );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}
